package com.abigdreamer.im.client.module.main.headtree;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;

import com.abigdreamer.im.client.module.vo.Group;
import com.abigdreamer.im.client.module.vo.MsgUser;
import com.abigdreamer.swing.app.SwingUtil;
import com.abigdreamer.swing.component.JCLabel;
import com.abigdreamer.util.ImageUtil;

public class EIMTree extends Box implements MouseListener {

	private static final long serialVersionUID = 8470772136329202294L;
	
	static ImageIcon collapsedIamge = new ImageIcon(ImageUtil.getSubImage(SwingUtil.getImage("arrows.gif"), 0, 0, 16, 16));
	static ImageIcon expandedIamge = new ImageIcon(ImageUtil.getSubImage(SwingUtil.getImage("arrows.gif"), 16, 0, 16, 16));

	private Group dataResoure;
	private JCLabel treeInfo;
	private boolean isexp = false;
	private List<EIMTreeNode> treeOnlineNodes = new ArrayList<EIMTreeNode>();
	Box userBox = Box.createVerticalBox();
	private List<EIMTreeNode> treeOffNodes = new ArrayList<EIMTreeNode>();

	public EIMTree() {
		super(BoxLayout.Y_AXIS);
		priantTree();
	}

	public EIMTree(Group dataResoure) {
		super(BoxLayout.Y_AXIS);
		this.dataResoure = dataResoure;
		priantTree();
	}

	public void priantTree() {
		this.setAlignmentX(Box.LEFT_ALIGNMENT);
		userBox.setAlignmentX(Box.LEFT_ALIGNMENT);
		
		if (dataResoure == null) {
			treeInfo = new JCLabel(" 未命名   [0/0]");
			treeInfo.setIcon(collapsedIamge);
			Box box = Box.createHorizontalBox();
			box.add(treeInfo, BorderLayout.WEST);
			box.add(new JCLabel("               "));
			this.add(box, BorderLayout.WEST);
		} else {
			initUsers();
			treeInfo = new JCLabel(" " + dataResoure.getGroupName()+ " ");
			treeInfo.setIcon(collapsedIamge);
			Box box = Box.createHorizontalBox();
			box.add(treeInfo, BorderLayout.WEST);
			int onlineNum = treeOnlineNodes.size();
			int allNum = treeOffNodes.size() + onlineNum;
			box.setAlignmentX(Box.LEFT_ALIGNMENT);
			box.add(new JCLabel("[" + onlineNum + "/" + allNum + "]"));

			this.add(box, BorderLayout.WEST);

			for (int i = 0; i < treeOnlineNodes.size(); i++) {
				userBox.add(treeOnlineNodes.get(i));
//				this.add(userBox);
			}
			for (int i = 0; i < treeOffNodes.size(); i++) {
				userBox.add(treeOffNodes.get(i));
			}
			this.add(userBox);
		}
		treeInfo.addMouseListener(new LaleMouseAdapter());
	}

	public void initUsers() {
		for (int i = 0; i < dataResoure.getGrops().size(); i++) {
			MsgUser user = dataResoure.getGrops().get(i);
			EIMTreeNode treeNode = new EIMTreeNode(user);
			
			if (user.isOnline()) {
				treeOnlineNodes.add(treeNode);
			} else {
				treeOffNodes.add(treeNode);
			}
		}
	}

	public void mouseClicked(MouseEvent e) {

	}

	public void mouseEntered(MouseEvent e) {

	}

	public void mouseExited(MouseEvent e) {

	}

	public void mousePressed(MouseEvent e) {

	}

	public void mouseReleased(MouseEvent e) {

	}

	class LaleMouseAdapter extends MouseAdapter {

		public void mouseEntered(final MouseEvent e) {
			treeInfo.setForeground(Color.black);
		}

		public void mouseClicked(final MouseEvent e) {
			if (e.getClickCount() == 1 || e.getClickCount() == 2) {
				System.out.print("双击");
				if (isexp) {
					for (int i = 0; i < treeOnlineNodes.size(); i++) {
						treeOnlineNodes.get(i).setVisible(false);

					}
					for (int i = 0; i < treeOffNodes.size(); i++) {
						treeOffNodes.get(i).setVisible(false);
					}
					treeInfo.setIcon(collapsedIamge);
					isexp = false;
				} else {
					for (int i = 0; i < treeOnlineNodes.size(); i++) {
						treeOnlineNodes.get(i).setVisible(true);
					}
					for (int i = 0; i < treeOffNodes.size(); i++) {
						treeOffNodes.get(i).setVisible(true);
					}
					treeInfo.setIcon(expandedIamge);
					isexp = true;
				}

			}
		}

		public void mouseExited(final MouseEvent e) {
			treeInfo.setBackground(Color.white);
		}
	}

	public List<EIMTreeNode> getTreeOnlineNodes() {
		return treeOnlineNodes;
	}

	public void setTreeOnlineNodes(List<EIMTreeNode> treeOnlineNodes) {
		this.treeOnlineNodes = treeOnlineNodes;
	}

	public List<EIMTreeNode> getTreeOffNodes() {
		return treeOffNodes;
	}

	public void setTreeOffNodes(List<EIMTreeNode> treeOffNodes) {
		this.treeOffNodes = treeOffNodes;
	}

}
